/**
 * Created by IntelliJ IDEA.
 * User: p.popov
 * Date: 20.03.13
 * Time: 0:47
 * To change this template use File | Settings | File Templates.
 */
package  {
import flash.geom.Point;
import starling.display.DisplayObject;
import starling.display.Image;
import starling.display.Sprite;
import starling.events.Event;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;

import slicesPanels.*

public class CSprite extends Sprite {

    /** обычная видимость */
    private var _visible:Boolean = true;
    /** видимость по разрешению */
    private var _permitted:Boolean = true;

    private var _destroyed:Boolean;

	/** поднятый флаг позволяет обрабатывать мышинные события, какбудта это нормальный дисплей обджект во флеше */
	private var _buttonMode:Boolean = false;
	private var hasMoved:Boolean = false;
	
	public function CSprite()
	{
		addEventListener(Event.ADDED_TO_STAGE, onAddedToStageHandler);
	}
	
	private function onAddedToStageHandler(e:Event):void 
	{
		removeEventListener(Event.ADDED_TO_STAGE, onAddedToStageHandler);
		addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStageHandler);
		
		addedToStage();
	}
	
	protected function addedToStage():void {
	}
	
	private function onRemovedFromStageHandler(e:Event):void 
	{
		removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStageHandler);
		addEventListener(Event.ADDED_TO_STAGE, onAddedToStageHandler);
		
		removedFromStage();
	}
	
	protected function removedFromStage():void {
	}

    public function getPhysicalPosition():Size {
        return new Size(x, y);
    }

    public function setPositionPoint(position:Point):void {
        x = position.x;
        y = position.y;
    }

    public function setPosition(x:Number, y:Number):void {
        this.x = x;
        this.y = y;
    }

    public function setCommonScale(scale:Number):void {
        scaleX = scaleY = scale;
    }

    public function setScale(scaleX:Number, scaleY:Number):void {
        this.scaleX = scaleX;
        this.scaleY = scaleY;
    }

    public function setSize(size:Size):void {
        this.width = size.width;
        this.height = size.height;
    }

    public function getSize():Size {
        return new Size(width, height);
    }

    public function permitShowing(value:Boolean):void {
        _permitted = value;
        updateVisibility();
    }

    override public function set visible(value:Boolean):void {
        _visible = value;
        updateVisibility();
    }

    private function updateVisibility():void {
        super.visible = _visible && _permitted;
    }

	/** установить цвет всем детям */
	public function setChildrenColor(color:int):void {
		for (var i:int = 0; i < this.numChildren; i++) {
            var displayObject: DisplayObject = this.getChildAt(i);
            if (displayObject is CSprite) {
                (displayObject as CSprite).setChildrenColor(color);
            }
			if (displayObject is Image) {
                (displayObject as Image).color = color;
            }

		}
	}

    public function destroy():void {
        _destroyed = true;
    }

    public function isDestroyed():Boolean {
        return _destroyed;
    }

	public function set buttonMode(value:Boolean):void {
		_buttonMode = value;
		updateTouchListeners();
	}

	public function get buttonMode():Boolean {
		return _buttonMode;
	}

	/** TODO: было бы благороднее перенести сей код в отдельный класс,
	 *  как делают это в модных домах лондану и парижу
	 * */
	public function updateTouchListeners():void {
		/*if(_buttonMode) {
			addEventListener(TouchEvent.TOUCH, onTouchHandler);
		} else {
			removeEventListener(TouchEvent.TOUCH, onTouchHandler);
		}*/
	}

	/*private function onTouchHandler(event:TouchEvent):void {
		const touch:Touch = event.getTouch(this);
		if(touch) {
			var touchPoint:Point = new Point(touch.globalX, touch.globalX);
			if(touch.phase == TouchPhase.BEGAN) {
				hasMoved = false;
				dispatchEvent(new MouseEvent(MouseEvent.DOWN, touchPoint));
			} else if(touch.phase == TouchPhase.HOVER) {
				dispatchEvent(new MouseEvent(MouseEvent.OVER, touchPoint));
			} else if(touch.phase == TouchPhase.ENDED) {
				dispatchEvent(new MouseEvent(MouseEvent.UP, touchPoint));
				if(!hasMoved) {
					if(touch.tapCount == 2)
						dispatchEvent(new MouseEvent(MouseEvent.DOUBLE_CLICK, touchPoint));
					else						
						dispatchEvent(new MouseEvent(MouseEvent.CLICK, touchPoint));
				} else {
					dispatchEvent(new MouseEvent(MouseEvent.END_MOVE, touchPoint));
				}
			} else if(touch.phase == TouchPhase.MOVED) {
				hasMoved = true;
				dispatchEvent(new MouseEvent(MouseEvent.MOVE, touchPoint));
			}
		}

	}*/
}
}
